package  
{
	import flash.display.Sprite;
	
	import com.bit101.components.CheckBox;
	import com.bit101.components.InputText;
	import com.bit101.components.PushButton;
	import com.bit101.components.Label;
	import com.bit101.components.Window;
	import com.bit101.components.FPSMeter;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author yaempechenki@gmail.com
	 */
	public class SettingView extends Sprite 
	{
		//http://screensiz.es/phone
		
		/** controls */
		private var serverUrlTF:InputText;
		private var deviceUrlTF:InputText;
		private var offlineModeCB:CheckBox;
		private var editorModeCB:CheckBox;
		private var forPveCB:CheckBox;
		private var editorModeForPveCB:CheckBox;
		private var drawTilesCB:CheckBox;
		private var ppiTF:InputText;
		private var widthTF:InputText;
		private var heightTF:InputText;
				
				
		private var saveButton:PushButton;
		public function SettingView(parentWidth:int, parentHeigth:int) 
		{
			//title = "Settings";
			//hasMinimizeButton = false;
			//width = stage.stageWidth;
			//height = stage.stageHeight;
			//draggable = false;
			
			
			deviceUrlTF = new InputText(this, 10, getPosY(), SettingsHolder.DEVICE_ID , onTFChange); 
			createLabel(deviceUrlTF.x + deviceUrlTF.width, deviceUrlTF.y, "user id");
			
			serverUrlTF = new InputText(this, 10, getPosY(), SettingsHolder.SERVER_URL, onTFChange); 
			createLabel(serverUrlTF.x + serverUrlTF.width, serverUrlTF.y, "url");
			
			offlineModeCB = new CheckBox(this, 10, getPosY(), "offline mode", onTFChange);
			offlineModeCB.selected = SettingsHolder.OFFLINE_MODE;
			
			editorModeCB = new CheckBox(this, 10, getPosY(), "editor mode", onTFChange);
			editorModeCB.selected = SettingsHolder.EDITOR_MODE;
			
			forPveCB = new CheckBox(this, 10, getPosY(), "for pve", onTFChange);
			forPveCB.selected = SettingsHolder.FOR_PVE;
			
			editorModeForPveCB = new CheckBox(this, 10, getPosY(), "editor mode for pve", onTFChange);
			editorModeForPveCB.selected = SettingsHolder.EDITOR_MODE_FOR_PVE;
			
			drawTilesCB = new CheckBox(this, 10, getPosY(), "draw tiles", onTFChange);
			drawTilesCB.selected = SettingsHolder.EDITOR_MODE_FOR_PVE;
			
			ppiTF = new InputText(this, 10, getPosY(), SettingsHolder.PPI.toString() , onTFChange); 
			createLabel(ppiTF.x + ppiTF.width, ppiTF.y, "ppi");
			
			widthTF = new InputText(this, 10, getPosY(), SettingsHolder.WIDTH.toString(), onTFChange); 
			createLabel(widthTF.x + widthTF.width, widthTF.y, "width");
			
			heightTF = new InputText(this, 10, getPosY(), SettingsHolder.HEIGHT.toString(), onTFChange); 
			createLabel(heightTF.x + heightTF.width, heightTF.y, "height");
			
			saveButton = new PushButton(this, 0, 0, "save", onSaveClick);
			saveButton.x = 10;
			saveButton.y = getPosY() + 10;
		}
		
		public function refresh():void 
		{
			deviceUrlTF.text = SettingsHolder.DEVICE_ID;
			serverUrlTF.text = SettingsHolder.SERVER_URL;
			
			offlineModeCB.selected = SettingsHolder.OFFLINE_MODE;
			forPveCB.selected = SettingsHolder.FOR_PVE;
			editorModeCB.selected = SettingsHolder.EDITOR_MODE;
			editorModeForPveCB.selected = SettingsHolder.EDITOR_MODE_FOR_PVE;
			drawTilesCB.selected = SettingsHolder.DRAW_LAND_TILES;
			
			ppiTF.text = SettingsHolder.PPI.toString();
			widthTF.text = SettingsHolder.WIDTH.toString();
			heightTF.text = SettingsHolder.HEIGHT.toString();
		}
		
		private function createLabel(toPosX:int, toPoxY:int, title:String) : void {
			var oneLabel:Label = new Label(this, toPosX + 5, toPoxY, title);
		}
		
		private function onTFChange(event:Event):void {
			SettingsHolder.SERVER_URL = serverUrlTF.text;
			SettingsHolder.DEVICE_ID = deviceUrlTF.text;
			SettingsHolder.OFFLINE_MODE = offlineModeCB.selected;
			SettingsHolder.FOR_PVE = forPveCB.selected;
			SettingsHolder.EDITOR_MODE = editorModeCB.selected;
			SettingsHolder.EDITOR_MODE_FOR_PVE = editorModeForPveCB.selected;
			SettingsHolder.DRAW_LAND_TILES = drawTilesCB.selected;
			SettingsHolder.PPI = int(ppiTF.text);
			SettingsHolder.WIDTH = int(widthTF.text);
			SettingsHolder.HEIGHT = int(heightTF.text);
		}
		
		private function onSaveClick(event:MouseEvent) :void {
			dispatchEvent(new Event(Event.SELECT));
		}
		
		private var yPositionIndex:int = 0;
		private function getPosY():int {
			var pos:int = yPositionIndex * 20 + 10;
			yPositionIndex++;
			return pos;
		}
		
	}

}